package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import util.GetTime;

public class FreeCommentDAO {
	
	public FreeComment insert(FreeComment fc)  {
		Connection conn = ConnectionManager.getConnection();
//@@	String sql1 = "insert into freecomment(fbno, mid, fccomment, fcdate, iconno) values(?,?,?,now(),?)";
		String sql1 = "insert into freecomment(fcno,fbno, mid, fccomment, fcdate, iconno) values(fcseq.NEXTVAL,?,?,?,?,?)";//##
		String sql2 = "select fcno,fbno,mid,fccomment,fcdate,iconno from freecomment where fcno=(select max(fcno) from freecomment)";
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(sql1);
			PreparedStatement pstmt2 = conn.prepareStatement(sql2);
			
			pstmt.setInt(1, fc.getFbno());
			pstmt.setString(2, fc.getMid());
			pstmt.setString(3, fc.getFccomment());
			pstmt.setTimestamp(4, GetTime.nowTime);//##
//			pstmt.setInt(4, fc.getIconno());
			pstmt.setInt(5, fc.getIconno());
			
			if(pstmt.executeUpdate()>0){
				ResultSet rs = pstmt2.executeQuery();
				FreeComment freeComment = new FreeComment();
				while(rs.next()){					
					freeComment.setFcno(rs.getInt(1));
					freeComment.setFbno(rs.getInt(2));
					freeComment.setMid(rs.getString(3));
					freeComment.setFccomment(rs.getString(4));
					freeComment.setFcdate(rs.getString(5));
					freeComment.setIconno(rs.getInt(6));
				}
				return freeComment;	
			}
			///////////////////////////////////////////////
			
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return null;
	}
	
	public boolean delete(int fcno) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			stmt.executeUpdate("delete from freecomment where fcno=" + fcno);
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}

	public boolean deleteAll(long fbno) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			stmt.executeUpdate("delete from freecomment where fbno=" + fbno);
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}
	
	public Vector<FreeComment> getFreeCommentList(int fbno){
		Vector<FreeComment> vector = new Vector<FreeComment>();
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(
					"select * from freecomment where fbno="+fbno);
			
			while(rs.next()) {
				FreeComment freeComment = new FreeComment();
				freeComment.setFcno(rs.getInt(1));
				freeComment.setFbno(rs.getInt(2));
				freeComment.setMid(rs.getString(3));
				freeComment.setFccomment(rs.getString(4));
				freeComment.setFcdate(rs.getString(5));
				freeComment.setIconno(rs.getInt(6));
				vector.add(freeComment);
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}
	
	public int getTotalRows(int fbno) {
		int result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select count(*) from freecomment where fbno="+fbno);
			rs.next();
			result = rs.getInt(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}	

}
